//
//  ActivityMineHonourController.m
//  ZiJinLian
//
//  Created by 震霄 张 on 2018/4/6.
//  Copyright © 2018年 lzy. All rights reserved.
//

#import "ActivityMineHonourController.h"
#import "AcvHonourTableViewCell.h"
#import "AcvMineHonourListModel.h"
@interface ActivityMineHonourController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,assign)NSInteger currentPage;
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,strong)AcvMineHonourListModel *model;
@end

@implementation ActivityMineHonourController
static NSString *const ZXActivityCellID = @"ZXActivityHonourCell";
-(NSMutableArray *)dataArr
{
	if (_dataArr == nil) {
		_dataArr = [NSMutableArray array];
	}
	return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
	[self setupUI];
	self.currentPage = 0;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)setupUI
{
	self.navigationItem.title = @"我的功德";
	UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, screen_Width, screen_Height-RD_NavBarHeight) style:UITableViewStylePlain];
	tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
	tableview.delegate = self;
	tableview.dataSource = self;
	[tableview registerClass:[AcvHonourTableViewCell class] forCellReuseIdentifier:ZXActivityCellID];
	tableview.rowHeight = 70;
	[self.view addSubview:tableview];
	self.tableView = tableview;
	
	tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
		
		[self setupDataWithCurrentPage:1];
	}];
	self.tableView.mj_footer.automaticallyHidden = YES;
	tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
		
		[self setupDataWithCurrentPage:self.currentPage+1];
		
	}];
	[tableview.mj_header beginRefreshing];
}
-(void)setupDataWithCurrentPage:(NSInteger)page
{
	NSMutableDictionary *dic=[[NSMutableDictionary   alloc]init];
	dic[@"pageSize"]=@"10";
	dic[@"pageNumber"]=[NSString stringWithFormat:@"%ld",page];
	dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];

	[self doHttpRequestWithApi:API_MINE_HONOUR_LIST method:GET dic:dic successBlock:^(NSDictionary *dic) {
		NSLog(@"%@",dic);
		[self.tableView.mj_header endRefreshing];
		[self.tableView.mj_footer endRefreshing];
		
		if ([[dic valueForKey:@"status"] integerValue] == 0) {
			self.model = [AcvMineHonourListModel mj_objectWithKeyValues:dic[@"data"]];
			
			self.currentPage = self.model.pageNumber;
			
			if (self.model.firstPage) {
				[self.dataArr removeAllObjects];
				[self.dataArr addObjectsFromArray:self.model.list];
			}else{
				[self.dataArr addObjectsFromArray:self.model.list];
			}
			
			if (self.model.lastPage) {
				self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
			}
			
			//---
			if (self.model.list.count == 0) {
				[ShowMainView showWithMsg:@"暂无记录"];
			}
			
			[self.tableView reloadData];
		}else{
			[MBProgressHUD showError:dic[@"msg"]];
		}
		
	}];
	
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	
	return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	AcvHonourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ZXActivityCellID forIndexPath:indexPath];
	cell.model = self.dataArr[indexPath.row];
	return cell;
}
@end
